﻿using System;
using System.Data.SqlClient;

namespace Framework.Sql
{
    public class SqlParameterAdder
    {
        private readonly string _connectionString;
        private readonly SqlCommand _command;

        public SqlParameterAdder(string connectionString, string sql)
        {
            _connectionString = connectionString;
            _command = new SqlCommand(sql);
        }

        public SqlParameterAdder AddParameter(string columnName, object value)
        {
            _command.Parameters.Add(new SqlParameter(columnName, value));
            return this;
        }

        public int Exec()
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();
                _command.Connection = connection;
                var result = _command.ExecuteScalar();
                _command.Dispose();
                connection.Close();

                return Convert.ToInt32(result);
            }
        }
    }
}